<template>
  <div class="filter-page">
    <header class="header">
      <button class="back-btn" @click="$router.push('/back')">
        <span>‹</span>
        返回
      </button>
      <h1>歌单过滤</h1>
    </header>
    
    <div class="content">
      <div v-if="loading" class="loading">
        <div class="spinner"></div>
        <p>加载中...</p>
      </div>
      
      <div v-else class="playlist-container">
        <!--div class="section-title">我的歌单</div-->
        <div class="playlist-list">
          <div 
            v-for="(isChecked, name) in playList" 
            :key="name" 
            class="playlist-item" 
            :class="{ checked: isChecked }"
            @click="togglePlayList(name)"
          >
            <div class="checkbox" :class="{ checked: isChecked }">
              <div class="checkmark" v-if="isChecked">✓</div>
            </div>
            <div class="playlist-info">
              <div class="playlist-name">{{ name }}</div>
              <!--div class="playlist-status">{{ isChecked ? '已显示' : '已隐藏' }}</div-->
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiService } from '../utils/http'
import { tools } from '../utils/tools'

export default {
  name: 'SongListFilter',
  data() {
    return {
      playList: {}
    }
  },
  async mounted() {
    await this.queryPlayListFilter()
  },
  methods: {
    async queryPlayListFilter() {
      try {
        const res = await apiService.getFresh('/QueryPlayListFilter')
        this.playList = res.data.playList
      } catch (error) {
        console.error('获取歌单失败:', error)
      } finally {
      }
    },
    
    async togglePlayList(item) {
      this.playList[item] = !this.playList[item]
      await this.updatePlayStatus(item)
    },
    
    async updatePlayStatus(item) {
      try {
        const headers = {token: `${tools.getTokenFromCookie()}`}
        await tools.authApiCall(
          () => apiService.post('/UpdatePlayStatus',
            { value: { [item]: this.playList[item] } }, { headers }),
          this.$router,
          this.$route
        )
      } catch (error) {
        console.error('更新状态失败:', error)
      }
    }
  }
}
</script>

<style scoped>
.filter-page {
  min-height: 100vh;
  background: #f8f9fa;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  color: white;
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: background 0.3s ease;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.header h1 {
  font-size: 20px;
  font-weight: 600;
}

.content {
  padding: 20px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e0e0e0;
  border-top: 3px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
  padding-left: 8px;
}

.playlist-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.playlist-item {
  background: white;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid #e0e0e0;
}

.playlist-item.checked {
  background: #f0f0f0;
  border-color: #667eea;
}

.playlist-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.checkbox {
  width: 20px;
  height: 20px;
  border: 2px solid #ccc;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.checkbox.checked {
  background: #667eea;
  border-color: #667eea;
}

.checkmark {
  color: white;
  font-size: 14px;
  font-weight: bold;
}

.playlist-info {
  flex: 1;
}

.playlist-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 2px;
}

.playlist-status {
  font-size: 12px;
  color: #666;
}
</style>
